package org.zfes.snowier.zbss.xzy.biz.solution.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZStrUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import com.google.common.collect.Lists;
import org.zfes.snowier.core.data.SelectVo;
import org.zfes.snowier.zbss.xzy.biz.solution.dao.AlgorithmMapper;
import org.zfes.snowier.zbss.xzy.biz.solution.model.Algorithm;
import org.zfes.snowier.zbss.xzy.biz.solution.service.IAlgorithmConfigItemService;
import org.zfes.snowier.zbss.xzy.biz.solution.service.IAlgorithmService;

import java.util.stream.Stream;
@Service
public class AlgorithmServiceImpl implements IAlgorithmService{
 	@Autowired
	private AlgorithmMapper algorithmMapper;
	
 	@Autowired IAlgorithmConfigItemService algorithmConfigItemService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createAlgorithm(Algorithm algorithm) {
		ZBeanUtil.setCreateInfo(algorithm);
		ZBeanUtil.validateBean(algorithm);
		
		Object id1=algorithmMapper.selectByCode(algorithm.getCode());
		if(id1!=null){
			ZAlert.Error("编码已经存在");
		}
		algorithmMapper.insertSelective(algorithm);
		
		algorithmConfigItemService.createRootConfigItem(algorithm.getId());
		
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Algorithm> loadAlgorithmById(Long id) {
		if(id!=null){
			return Optional.ofNullable(algorithmMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public  Optional<Algorithm> loadAlgorithmByCode(String code){
		if(ZStrUtil.hasText(code)){
			return Optional.ofNullable(algorithmMapper.selectByCode(code));
		}
		return Optional.empty();
	 }
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateAlgorithm(Algorithm algorithm) {
		ZBeanUtil.setUpdatedInfo(algorithm);
		ZBeanUtil.validateBean(algorithm);
		Algorithm id1=algorithmMapper.selectByCode(algorithm.getCode());
		if(id1!=null&&!id1.getId().equals(algorithm.getId())){
			ZAlert.Error("编码已经存在");
		}
		algorithmMapper.updateByPrimaryKeySelective(algorithm);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteAlgorithm(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		algorithmMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setAlgorithmStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		algorithmMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadAlgorithmDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(algorithmMapper.selectListCount(pm), algorithmMapper.selectMapListPage(pm));
		return ds;
	}
	

	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<SelectVo> loadAlgorithmSelectVo(String keywords) {
		if(ZStrUtil.hasText(keywords)) {
			
		}
		List<SelectVo> voList=algorithmMapper.selectDropList(Byte.valueOf("1"));
		
		return voList==null?Lists.newArrayList():voList;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Algorithm> loadAllAlgorithm(Byte status) {
		return algorithmMapper.selectAllAlgorithm(null,null, status);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Algorithm> loadAlgorithmByName(String name , Integer maxCount , Byte status) {
		String nameStr=ZStrUtil.hasNoText(name)?null:name+"%";
		if(maxCount==null) {
			maxCount=20;
		}
		return algorithmMapper.selectAllAlgorithm(nameStr,maxCount, status);
	}
	
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Algorithm> loadByIds(List<Long> algorithmIds){
		if(ZObjectUtil.isEmpty(algorithmIds)) {
			return Lists.newArrayList();
		}
		return algorithmMapper.selectByIds(algorithmIds);
	}
}